package com.bigcat.gulimall.gulimallsearch.controller;

import com.bigcat.gulimall.common.entry.SkuEsModel;
import com.bigcat.gulimall.common.exception.BizCodeEnum;
import com.bigcat.gulimall.common.utils.R;
import com.bigcat.gulimall.gulimallsearch.service.ElasticSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Ather:Yuan
 * @Date:2021/1/4
 * @Desc:
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {

    @Autowired
    private ElasticSaveService elasticSaveService;

    /**
     * 保存
     * @param esModelList
     * @return
     */
    @PostMapping("product")
    public R productStatusUp(@RequestBody List<SkuEsModel> esModelList) {
        boolean result = false;
        try {
            result = elasticSaveService.productStatusUp(esModelList);
        } catch (Exception e) {
            log.error("商品上架失败", e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (result) {
            R  r  = new R();
            r.setCode(0);
            return r;
        } else {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }
}
